import 'dart:convert';

import 'package:inmotion_pro/pages/models/base_model.dart';
import 'package:inmotion_pro/service/realtime_data_server/vehicle_realtime_data/vehicle_statistics_once.dart';
import 'package:safemap/safemap.dart';

// 实时信息Item模型
class RealtimeItemModel extends BaseModel {
  // 标题
  String title = '';
  // 值
  String value = '';
  // 图片路径
  String imgPath = '';
  // 单位
  String unit = '';
  // 允许编辑
  bool isEditable = false;
  // 是否选中
  bool isSelected = false;
  // 选中索引
  int selectedIndex = 0;

  RealtimeItemModel.fromMap(Map<String, dynamic> map) {
    title = map['title'] ?? '';
    value = map['value'] ?? '';
    imgPath = map['imgPath'] ?? '';
    unit = map['unit'] ?? '';
    isEditable = map['isEditable'] ?? false;
    isSelected = map['isSelected'] ?? false;
    selectedIndex = map['selectedIndex'] ?? 0;
  }

  // copyWith方法
  RealtimeItemModel copyWith({
    String? title,
    String? value,
    String? imgPath,
    String? unit,
    bool? isEditable,
    bool? isSelected,
  }) {
    return RealtimeItemModel.fromMap({
      'title': title ?? this.title,
      'value': value ?? this.value,
      'imgPath': imgPath ?? this.imgPath,
      'unit': unit ?? this.unit,
      'isEditable': isEditable ?? this.isEditable,
      'isSelected': isSelected ?? this.isSelected,
      'selectedIndex': selectedIndex,
    });
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'title': title,
      'value': value,
      'imgPath': imgPath,
      'unit': unit,
      'isEditable': isEditable,
      'isSelected': isSelected,
      'selectedIndex': selectedIndex,
    };
  }
}

// 缓存的实时信息列表项模型
class CacheRealtimeItemModel {
  // bleName
  String? bleName;

  // sn
  String? sn;

  // 剩余预估里程
  double? estimatedRemainingMileage;

  // 总里程
  double? totalMileage;

  // 电池电量百分比
  double? batteryPercentage;

  // 索引列表（依据MainConstant.totalRealtimeItemList索引而定）
  List<RealtimeItemModel>? itemList;

  // 最新骑行记录
  VehicleStatisticsOnce? cyclingRecord;

  CacheRealtimeItemModel.fromMap(Map<String, dynamic>? map) {
    SafeMap safeMap = SafeMap(map);
    bleName = safeMap['ble_name'].string;
    sn = safeMap['sn'].string;
    estimatedRemainingMileage =
        safeMap['estimated_remaining_mileage'].toDouble ?? 0.0;
    totalMileage = safeMap['total_mileage'].toDouble ?? 0.0;
    batteryPercentage = safeMap['battery_percent'].toDouble ?? 0.0;
    // 解析item_list实时数据
    if (safeMap['item_list'].string != null &&
        safeMap['item_list'].string!.isNotEmpty) {
      itemList = (jsonDecode(safeMap['item_list'].string!) as List<dynamic>)
          .map(
              (item) => RealtimeItemModel.fromMap(item as Map<String, dynamic>))
          .toList();
    }

    // 解析cycling_record最近骑行记录
    if (safeMap['cycling_record'].string != null &&
        safeMap['cycling_record'].string!.isNotEmpty) {
      cyclingRecord = VehicleStatisticsOnce.fromMap(
          jsonDecode(safeMap['cycling_record'].string!)
              as Map<String, dynamic>);
    }
  }
}
